package com.jeomo.common.core.result;

import java.io.Serializable;

/**
 * 通用的返回结果
 * @author QBT
 */
public class CommonResult<X> implements Serializable {

    private Integer code;

    private String msg;

    private X data;

    final static Integer SUCCESS_CODE = 20000;
    final static String SUCCESS_MSG = "成功";

    final static Integer DEFAULT_FAILURE_CODE = 50000;
    final static String DEFAULT_FAILURE_MSG = "操作失败";

    /**
     * 成功返回
     * @return
     */
    public static  CommonResult success() {
        CommonResult result = new CommonResult();
        result.setCode(SUCCESS_CODE);
        result.setMsg(SUCCESS_MSG);
        return result;
    }


    /**
     * 成功返回
     * @param data
     * @param <T>
     * @return
     */
    public static <T> CommonResult<T> success(T data) {
        CommonResult<T> result = new CommonResult<T>();
        result.setCode(SUCCESS_CODE);
        result.setMsg(SUCCESS_MSG);
        result.setData(data);
        return result;
    }

    /**
     * 失败返回
     * @param <T>
     * @return
     */
    public static <T> CommonResult<T> failure() {
        CommonResult<T> result = new CommonResult<T>();
        result.setCode(DEFAULT_FAILURE_CODE);
        result.setMsg(DEFAULT_FAILURE_MSG);
        return result;
    }

    /**
     * 失败返回
     * @param <T>
     * @return
     */
    public static <T> CommonResult<T> failure(String msg) {
        CommonResult<T> result = new CommonResult<T>();
        result.setCode(DEFAULT_FAILURE_CODE);
        result.setMsg(msg);
        return result;
    }


    /**
     * 失败返回
     * @param <T>
     * @return
     */
    public static <T> CommonResult<T> failure(Integer errorCode, String msg) {
        CommonResult<T> result = new CommonResult<T>();
        result.setCode(errorCode);
        result.setMsg(msg);
        return result;
    }

    /**
     * 失败返回
     * @param <T>
     * @return
     */
    public static <T> CommonResult<T> failure(ResultCode resultCode) {
        CommonResult<T> result = new CommonResult<T>();
        result.setCode(resultCode.code());
        result.setMsg(resultCode.msg());
        return result;
    }

    //**********************getter setter 方法

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public X getData() {
        return data;
    }

    public void setData(X data) {
        this.data = data;
    }
}
